package com.yongbao.config;

import com.google.common.collect.Lists;
import com.yongbao.common.interceptor.HeaderInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig  implements WebMvcConfigurer {

  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")
        .allowedHeaders("*")
        .allowedMethods("*")
        .allowedOrigins("*")
        .allowCredentials(true);
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(getHeaderInterceptor())
            .addPathPatterns("/admin/**", "/wechat/**")
            .excludePathPatterns(Lists.newArrayList("/swagger-resources/**"))
            .order(-10);
  }

  /**
   * 自定义请求头拦截器
   */
  public HeaderInterceptor getHeaderInterceptor() {
    return new HeaderInterceptor();
  }

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    /** 本地文件上传路径 */
    String path = System.getProperty("user.dir") + "/storage";
    System.out.println("path=" + path);
    registry.addResourceHandler("/profile/**").addResourceLocations("file:" + path);
  }
}
